package lu.med.praxiswahl.wahlumed.client.arztbriefe;

import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JTextField;

import lu.med.praxiswahl.wahlumed.client.utilities.ContextFactoryRemote;
import lu.med.praxiswahl.wahlumed.client.utilities.ContextFactoryRemoteInt;
import lu.med.praxiswahl.wahlumed.client.wclient.Main;
import lu.med.praxiswahl.wahlumed.service.beans.DoctorService;
import lu.med.praxiswahl.wahlumed.service.beans.serviceinterface.DoctorServiceInt;

@SuppressWarnings("serial")
public class HausarztCreator extends JDialog implements ActionListener
{
    private JTextField name, adresse, tel, fax, fachgebiet, vorname;
    private ArztbriefThread thread;
    private ArztbriefPanel owner;

    public HausarztCreator(Main dataManager, ArztbriefPanel owner,
	    String matricule, ArztbriefThread thread)
    {
	super(dataManager, "Hausarzt anlegen", true);
	this.setLayout(new GridLayout(0, 2));

	this.thread = thread;
	this.owner = owner;

	name = new JTextField("");
	vorname = new JTextField("");
	adresse = new JTextField("");
	tel = new JTextField("");
	fax = new JTextField("");
	fachgebiet = new JTextField("");
	this.add(new JLabel("Name: "));
	this.add(name);
	this.add(new JLabel("Vorname; "));
	this.add(vorname);
	this.add(new JLabel("Adresse: "));
	this.add(adresse);
	this.add(new JLabel("Fachgebiet: "));
	this.add(tel);
	this.add(new JLabel("Tel.: "));
	this.add(fax);
	this.add(new JLabel("Fax.: "));
	this.add(fachgebiet);
	JButton a, b;
	a = new JButton("Ok");
	a.addActionListener(this);
	b = new JButton("Abbrechen");
	b.addActionListener(this);
	this.add(a);
	this.add(b);
	this.pack();
	this.setVisible(true);
    }

    @Override
    public void actionPerformed(ActionEvent arg0)
    {
	if (arg0.getActionCommand().equals("Ok"))
	{
	    // Erstellt den Doctor
	    ContextFactoryRemoteInt remote_context = new ContextFactoryRemote();
	    DoctorServiceInt doc = remote_context
		    .getRemoteContextDoctorService();
	    doc = new DoctorService();
	    doc.createAddress(name.getText(), vorname.getText(), adresse
		    .getText(), "", tel.getText(), fax.getText(), fachgebiet
		    .getText());
	    // Speichert den Doctor in der Patientin
	    Long[] ids = doc.hausAerzteAnzeigen();
	    Long id = null;
	    for (int i = 0; i < ids.length; i++)
	    {
		doc.getDoctor(ids[i]);
		if (doc.getAddresse(ids[i]).equals(adresse.getText())
			&& doc.getNachname(ids[i]).equals(name.getText()))
		{
		    id = ids[i];
		    break;
		}
	    }
	    /*
	     * dataManager.getCustomerContext().getCustomer(matricule);
	     * dataManager
	     * .getCustomerContext().setHausArtz(doc.gethausArtzByID(id));
	     */
	    // Speichert die ID im Dialog
	    owner.setMostImportantDoctorID(id);
	    thread.setExitThis();
	    this.setVisible(false);
	}
	if (arg0.getActionCommand().equals("Abbrechen"))
	{
	    owner.setMostImportantDoctorID(null);
	    thread.setExitThis();
	    this.setVisible(false);
	}
    }

}
